//
//  ViewController.m
//  纵向图书章节进度条
//
//  Created by 梁森 on 2018/4/11.
//  Copyright © 2018年 梁森. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

{
    BOOL scrollSlider;  // 是否滑动了slider
}
@property (nonatomic, strong) UITableView *  tableView;
@property (nonatomic, strong) UISlider * slider;

@end

@implementation ViewController

/*
 BookChapterListVC
 
 */


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.tableView];
    
    [self addSlider];
    scrollSlider = NO;
}

- (void)addSlider{
//    UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 30 , 40, 20, self.tableView.frame.size.height)];
    UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 30)];
    [self.view addSubview:slider];
    slider.transform = CGAffineTransformMakeRotation(90 *M_PI / 180.0);
    slider.frame = CGRectMake(self.view.frame.size.width - 30, 40, slider.frame.size.width, self.tableView.frame.size.height);
    slider.minimumValue = 0;// 设置最小值
    slider.maximumValue = 1;// 设置最大值
    slider.value = 0;// 设置初始值
    slider.continuous = YES;// 设置可连续变化
//    slider.backgroundColor = [UIColor blackColor];
    slider.backgroundColor = [UIColor greenColor];
//    slider.minimumTrackTintColor = [UIColor clearColor]; //滑轮左边颜色，如果设置了左边的图片就不会显示
//    slider.maximumTrackTintColor = [UIColor clearColor]; //滑轮右边颜色，如果设置了右边的图片就不会显示
//    [slider setThumbImage:[UIImage imageNamed:@"sliderimage"] forState:UIControlStateNormal];
    [slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];// 针对值变化添加响应方法
    self.slider = slider;
}

// slider变动时改变label值
- (void)sliderValueChanged:(id)sender {
    scrollSlider = YES;
    UISlider *slider = (UISlider *)sender;
    float sliderValue = slider.value;
    NSInteger index = (400 - 1) * sliderValue;  // 数据源总数乘以滑动控件滑动的值
    NSIndexPath *scrollIndePath = [NSIndexPath indexPathForRow:index inSection:0];
    [self.tableView scrollToRowAtIndexPath:scrollIndePath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    // 开始拖拽scroll
    scrollSlider = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollSlider == YES) {  // 如果正在滑动slider控件
        
    }else{
        CGFloat y = scrollView.contentOffset.y;
        NSLog(@"y:%f", y);
        if (y >= 0) {
            //        NSLog(@"value:%f", y / (400 * 50));
            if (y < scrollView.frame.size.height) {
                self.slider.value =  y / (400 * 50);
            }else{
                self.slider.value =  (y+scrollView.frame.size.height) / (400 * 50);
            }
        }
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
#pragma mark --- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 400;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%lu", indexPath.row];
    return cell;
}


- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height-80) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
